package com.hnl.action.talent;

import java.io.IOException;
import java.text.ParseException;
import java.util.Date;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.hnl.core.HNLPageDispatch;
import com.hnl.core.aware.HNLAuthAware;
import com.hnl.core.aware.HNLTalentAware;
import com.hnl.core.aware.MiscTypeAware;
import com.hnl.dao.DAOException;
import com.hnl.dao.HNLDAOFactory;
import com.hnl.dao.TalentDAO;
import com.hnl.to.Talent;
import com.hnl.to.User;
import com.hnl.util.FormatUtil;

@SuppressWarnings("serial")
public class AddTalent extends HttpServlet implements HNLAuthAware,
		HNLTalentAware, MiscTypeAware, HNLPageDispatch {
	public static final Logger LOG = LoggerFactory.getLogger(AddTalent.class);

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		try {
			Integer talentID = Integer.valueOf(req
					.getParameter(ATTRIBUTE_TALENT_ID));
			Integer tabID = Integer.valueOf(req.getParameter(ATTRIBUTE_TAB_ID));

			HttpSession session = ((HttpServletRequest) req).getSession();
			User user = (User) session.getAttribute(ATTRIBUTE_USER);

			TalentDAO talentDAO = HNLDAOFactory.getDAOFactory(
					HNLDAOFactory.HNLDAOFACTORY).getTalentDAO();

			Talent talent;
			Date now = new Date();
			if (talentID.equals(0)) {
				talent = new Talent();
				talent.setCreateDate(now);
				talent.setCreateUser(user.getUserID());
				talent.setIsdelete(false);
			} else {
				talent = talentDAO.loadTalent(talentID);
			}

			/* Personal Info */
			if (req.getParameter("eFirstName") != null) {
				talent.seteFirstname(req.getParameter("eFirstName"));
			}
			if (req.getParameter("eLastName") != null) {
				talent.seteLastname(req.getParameter("eLastName"));
			}
			if (req.getParameter("cFirstName") != null) {
				talent.setcFirstname(req.getParameter("cFirstName"));
			}
			if (req.getParameter("cLastName") != null) {
				talent.setcLastname(req.getParameter("cLastName"));
			}
			if (req.getParameter("birthday") != null
					&& req.getParameter("birthday").length() > 0) {
				talent.setBirthdate(FormatUtil.getDateFormatUS().parse(
						req.getParameter("birthday")));
			}
			if (req.getParameter(PARAM_TALENT_GENDER) != null
					&& req.getParameter(PARAM_TALENT_GENDER).length() > 0) {
				talent.setGender(Integer.parseInt(req
						.getParameter(PARAM_TALENT_GENDER)));
			}
			if (req.getParameter(PARAM_TALENT_NATIONALITY) != null
					&& req.getParameter(PARAM_TALENT_NATIONALITY).length() > 0) {
				talent.setNationality(Integer.parseInt(req
						.getParameter(PARAM_TALENT_NATIONALITY)));
			}
			if (req.getParameter(PARAM_TALENT_RELIGION) != null
					&& req.getParameter(PARAM_TALENT_RELIGION).length() > 0) {
				talent.setReligion(Integer.parseInt(req
						.getParameter(PARAM_TALENT_RELIGION)));
			}
			if (req.getParameter(PARAM_TALENT_MARITIAL) != null
					&& req.getParameter(PARAM_TALENT_MARITIAL).length() > 0) {
				talent.setMaritalStatus(Integer.parseInt(req
						.getParameter(PARAM_TALENT_MARITIAL)));
			}
			if (req.getParameter("familyBackground") != null) {
				talent.setFamilyBackground(req.getParameter("familyBackground"));
			}
			if (req.getParameter("interest") != null) {
				talent.setInterest(req.getParameter("interest"));
			}
			if (req.getParameter("Address") != null) {
				talent.setAddress(req.getParameter("Address"));
			}
			if (req.getParameter("officeTel") != null) {
				talent.setOfficeTel(req.getParameter("officeTel"));
			}
			if (req.getParameter("did") != null) {
				talent.setDid(req.getParameter("did"));
			}
			if (req.getParameter("homeTel") != null) {
				talent.setHomePhone(req.getParameter("homeTel"));
			}
			if (req.getParameter("fax") != null) {
				talent.setFax(req.getParameter("fax"));
			}
			if (req.getParameter("cell1") != null) {
				talent.setCell1(req.getParameter("cell1"));
			}
			if (req.getParameter("cell2") != null) {
				talent.setCell2(req.getParameter("cell2"));
			}
			if (req.getParameter("comMail") != null) {
				talent.setComMail(req.getParameter("comMail"));
			}
			if (req.getParameter("personalMail") != null) {
				talent.setPersonalMail(req.getParameter("personalMail"));
			}
			if (req.getParameter("starRating") != null) {
				talent.setRating(req.getParameter("starRating"));
			} else {
				talent.setRating(String.valueOf(0));
			}
			if (req.getParameter("skype") != null) {
				talent.setSkype(req.getParameter("skype"));
			}
			if (req.getParameter("linkedin") != null) {
				talent.setLinkedin(req.getParameter("linkedin"));
			}
			if (req.getParameter("facebook") != null) {
				talent.setFacebook(req.getParameter("facebook"));
			}
			if (req.getParameter("plurk") != null) {
				talent.setPlurk(req.getParameter("plurk"));
			}
			if (req.getParameter("twitter") != null) {
				talent.setTwitter(req.getParameter("twitter"));
			}
			if (req.getParameter("others") != null) {
				talent.setOthers(req.getParameter("others"));
			}
			if (req.getParameter("additionalRef") != null) {
				talent.setAdditionalReferenceInfo(req
						.getParameter("additionalRef"));
			}

			/* Career Info */
			if (req.getParameter("currentCompany") != null) {
				talent.setCompany(req.getParameter("currentCompany"));
			}
			if (req.getParameter(PARA_TALENT_INDUSTRY1) != null
					&& req.getParameter(PARA_TALENT_INDUSTRY1).length() > 0) {
				talent.setIndustry1(Integer.parseInt(req
						.getParameter(PARA_TALENT_INDUSTRY1)));
			}
			if (req.getParameter(PARA_TALENT_INDUSTRY2) != null
					&& req.getParameter(PARA_TALENT_INDUSTRY2).length() > 0) {
				talent.setIndustry2(Integer.parseInt(req
						.getParameter(PARA_TALENT_INDUSTRY2)));
			}
			if (req.getParameter(PARA_TALENT_WORK_COUNTRY) != null
					&& req.getParameter(PARA_TALENT_WORK_COUNTRY).length() > 0) {
				talent.setCountry(Integer.parseInt(req
						.getParameter(PARA_TALENT_WORK_COUNTRY)));
			}
			if (req.getParameter("city") != null) {
				talent.setCity(req.getParameter("city"));
			}
			if (req.getParameter("position") != null) {
				talent.setPosition(req.getParameter("position"));
			}
			if (req.getParameter("jobFunction") != null) {
				talent.setJobfunction(req.getParameter("jobFunction"));
			}
			if (req.getParameter("salary") != null) {
				talent.setSalary(req.getParameter("salary"));
			}
			if (req.getParameter("yearsOfWork") != null) {
				talent.setTotalYearsOfWork(req.getParameter("yearsOfWork"));
			}
			if (req.getParameter("workExperience") != null) {
				talent.setWorkExperience(req.getParameter("workExperience"));
			}
			if (req.getParameter("overseas") != null) {
				talent.setOverseasExposure(req.getParameter("overseas"));
			}
			if (req.getParameter("expertise") != null) {
				talent.setExpertise(req.getParameter("expertise"));
			}
			if (req.getParameter("careerGoal") != null) {
				talent.setCareerGoal(req.getParameter("careerGoal"));
			}
			if (req.getParameter("techSkill") != null) {
				talent.setTechnicalSkill(req.getParameter("techSkill"));
			}
			if (req.getParameter("lanSkill") != null) {
				talent.setLanguageSkill(req.getParameter("lanSkill"));
			}
			if (req.getParameter("comment") != null) {
				talent.setComment(req.getParameter("comment"));
			}

			talent.setModifyDate(now);
			talent.setModifyUser(user.getUserID());

			if (talentID.equals(0)) {
				talentDAO.saveTalent(talent);
				talentID = talent.getTalentId();
			} else {
				talentDAO.updateTalent(talent);
			}

			req.setAttribute(ATTRIBUTE_TAB_ID, tabID);
			req.setAttribute(ATTRIBUTE_TALENT_ID, talentID);

			RequestDispatcher dispatcher = req
					.getRequestDispatcher(TALENT_INFO_PAGE);
			dispatcher.forward(req, resp);

		} catch (DAOException e) {
			LOG.error("Error to add talent.", e);
		} catch (ParseException e) {
			LOG.error("Error to add talent.", e);
		}

	}
}
